package web;

import java.util.Map;

import mongo.ItemDAO;
import vo.Item;
import vo.Source;
import vo.User;
import business.SourceService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class ItemAction implements Preparable {
	private User curUser;
	private UserService userService;
	private SourceService sourceService;
	private String itemId;
	private Item item;

	public String itemDetail() {
		System.out.println("item detail");
		System.out.println("itemId:" + itemId);
		try {
			item = ItemDAO.getById(itemId);
			Source source = sourceService.getById(Long.parseLong(item.getSource()));
			item.setSource(source.getName());
			System.out.println("item:" + item);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return Action.SUCCESS;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public void setSourceService(SourceService sourceService) {
		this.sourceService = sourceService;
	}

}